<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <el-menu
        :router="true"
        :default-active="$route.path"
        class="el-menu-vertical"
      >
        <el-menu-item index="/home/study-room">
          <el-icon><School /></el-icon>
          <span>自习室列表</span>
        </el-menu-item>
        <el-menu-item index="/home/reservation">
          <el-icon><Calendar /></el-icon>
          <span>我的预约</span>
        </el-menu-item>
        <el-menu-item index="/home/feedback">
          <el-icon><ChatDotRound /></el-icon>
          <span>留言反馈</span>
        </el-menu-item>
        <el-menu-item index="/home/announcement">
          <el-icon><Bell /></el-icon>
          <span>系统公告</span>
        </el-menu-item>
        <el-menu-item index="/home/snack">
          <el-icon><Food /></el-icon>
          <span>零食商城</span>
        </el-menu-item>
        <el-menu-item index="/home/order">
          <el-icon><ShoppingCart /></el-icon>
          <span>我的订单</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div class="header-left">
          <h2>自习室管理系统</h2>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ userInfo.username }}
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import {
  School,
  Calendar,
  ChatDotRound,
  Bell,
  Food,
  ShoppingCart,
  ArrowDown
} from '@element-plus/icons-vue'

export default {
  name: 'Home',
  components: {
    School,
    Calendar,
    ChatDotRound,
    Bell,
    Food,
    ShoppingCart,
    ArrowDown
  },
  setup() {
    const store = useStore()
    const router = useRouter()

    const userInfo = computed(() => store.state.userInfo)

    const handleCommand = (command) => {
      if (command === 'logout') {
        ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          store.dispatch('logout')
          router.push('/login')
        })
      } else if (command === 'profile') {
        // 跳转到个人中心
        router.push('/home/profile')
      }
    }

    const getImageUrl = (path) => {
      if (!path) return ''
      if (path.startsWith('http')) {
        return path
      }
      return `http://localhost:8080/api${path}`  // 添加/api前缀
    }

    return {
      userInfo,
      handleCommand,
      getImageUrl
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-header {
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #dcdfe6;
}

.el-aside {
  background-color: #304156;
}

.el-menu {
  border-right: none;
  background-color: #304156;
}

.el-menu-item {
  color: #bfcbd9;
}

.el-menu-item:hover {
  color: #fff;
}

.el-menu-item.is-active {
  color: #409eff;
}

.header-right {
  margin-right: 20px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
</style> 